
import { defineStore } from 'pinia';
import router from '@/router';
import { systemApi } from '@/api/system';

export const useMainStore = defineStore('main', {
  state: () => ({
    data: null as any,
    loading: false,
    error: null as string | null,
    token: localStorage.getItem('water-network-token') || null as string | null,
    userInfo: null as any,
  }),

  actions: {
    // 设置 token
    setToken(token: string) {
      this.token = token;
      localStorage.setItem('water-network-token', token);
    },

    // 获取 token（通常外部直接访问 this.token 即可）
    getToken(): string | null {
      return this.token;
    },

    // 删除 token
    removeToken() {
      this.token = null;
      localStorage.removeItem('water-network-token');
    },

    // 登出并跳转登录页
    logout() {
      this.removeToken();
      this.userInfo = null;
      router.push('/login');
    },
    setUserinfo(userinfo: any) {
      this.userInfo = userinfo;
    },
    getUserinfo() {
      return this.userInfo;
    },
    
  
  }
});